// pages/content/content.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    swiperImgList: [
      1, 2, 3, 4
    ],
    activeName: '',
    searchText: '',
    categoryId: 0, //分类id
    page: 1,
    size: 10,
    categories: [],
    nowIndex: -1,
  },


  toDetail(e) {
    var flag = e.currentTarget.dataset.flag,
      obj = e.currentTarget.dataset.obj;

    console.log(flag);
    console.log(obj);

    wx.navigateTo({
      url: '/contentPackage/pages/baikeDetail/baikeDetail?obj=' + JSON.stringify(obj),
    })
  },

  // 搜索
  toSearch(e) {
    wx.navigateTo({
      url: '/contentPackage/pages/baikeSearch/baikeSearch',
    })
  },


  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })

    console.log(this.data);
  },

  // 切换tab栏
  onActiveChange(e) {
    console.log(e);
    var index = e.detail.index,
      categories = this.data.categories;

    console.log(categories);
    console.log(index);
    console.log(categories[index]);

    this.setData({
      nowIndex: index
    })
    this.getIndex(categories[index].categoryId)
  },


  getIndex(id) {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "ScBaiKe/listBaiKe",
      categories = this.data.categories,
      nowIndex = this.data.nowIndex,
      that = this;
    app.wxRequest(url, {
        categoryId: id,
        page: nowIndex == -1 ? that.data.page : categories[nowIndex].page,
        size: that.data.size,
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          wx.stopPullDownRefresh()

          if (categories.length == 0) {
            datas.categories.forEach(item => {
              item.page = 1
              item.isFinish = false
              item.list = []
            })
            categories = datas.categories
            categories[0].list = datas.topicOrVideo.list
            nowIndex = 0
            categories[0].page = 2
          } else {
            if (datas.topicOrVideo.list.length <= that.data.size) {
              categories[nowIndex].isFinish = true
            }
            categories[nowIndex].page++
            categories[nowIndex].list.push(...datas.topicOrVideo.list)
          }
          console.log(datas);
          wx.hideLoading()
          console.log(categories);

          that.setData({
            categories,
            nowIndex
          })
        }
      },
      err => {
        console.log(err);
      })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.getIndex(0)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    var categories = this.data.categories,
      nowIndex = this.data.nowIndex;

    categories[nowIndex].page = 1
    categories[nowIndex].isFinish = false
    categories[nowIndex].list = []
    this.setData({
      categories
    })

    this.getIndex(categories[nowIndex].categoryId)

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    var categories = this.data.categories,
      nowIndex = this.data.nowIndex;

    if (categories[nowIndex].isFinish) {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    } else {
      this.getIndex(categories[nowIndex].categoryId)
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})